//var angular = require('angular');

//配置动态加载
angular.module('imgApp').config(["$provide", "$compileProvider", "$controllerProvider", "$filterProvider",
	function($provide, $compileProvider, $controllerProvider, $filterProvider) {
		angular.module('imgApp').controller = $controllerProvider.register;
		angular.module('imgApp').directive = $compileProvider.directive;
		angular.module('imgApp').filter = $filterProvider.register;
		angular.module('imgApp').factory = $provide.factory;
		angular.module('imgApp').service = $provide.service;
		angular.module('imgApp').constant = $provide.constant;
	}
]);

angular.module('imgApp').config(function($httpProvider) {
	$httpProvider.defaults.useXDomain = true;
//	$httpProvider.defaults.cache = true;
	delete $httpProvider.defaults.headers.common['X-Requested-With'];
});

//配置ui-router
angular.module('imgApp').config(function($stateProvider) {
	$stateProvider
	   .state('home', {
	   	    url: '/home',
	    	templateUrl: 'home/home.html',
	    	controller: 'HomeCtrl',
	    	resolve: {
	    		loadMyCtrl: ['$ocLazyLoad', function($ocLazyLoad) {
	    			return $ocLazyLoad.load(["home/home.css", 
	    			                         "home/home.controller.js",
	    			                         "lib/directive/pagination/pagination.directive.js"
	    			]);
	    		}]
	    	}
	   })
	   .state('user', {
	   	    url: '/user',
	    	templateUrl: 'user-page/user-page.html',
	    	controller: 'UserCtrl',
//	    	cache: false,
	    	resolve: {
	    		loadMyCtrl: ['$ocLazyLoad', function($ocLazyLoad) {
	    			return $ocLazyLoad.load(["user-page/user-page.css", 
	    			                         "user-page/user-page.controller.js"]);
	    		}]
	    	}
	   }).state('home.collections', {
	   	    url: '/collection/:img',
	    	templateUrl: 'user-page/user-page.html',
	    	controller: 'UserCtrl',
	    	resolve: {
	    		loadMyCtrl: ['$ocLazyLoad', function($ocLazyLoad) {
	    			return $ocLazyLoad.load(["user-page/user-page.css", 
	    			                         "user-page/user-page.controller.js"]);
	    		}]
	    	}
	   });

});

//app运行时，检查是有验证过
angular.module('imgApp').run(function ($rootScope, $state) {
	$rootScope.$on('$stateChangeStart',
			function(event, toState, toParams, fromState, fromParams) {
				console.log(event);
				console.log(toState);
				console.log(toParams);
				console.log(fromState);
				console.log(fromParams);
				if(toState.name == "home") {
					//获取参数之后可以调请求判断需要渲染什么页面，渲染不同的页面通过 $location 实现
					if(toParams.id == 10) {
						//$location.path();//获取路由地址
						// $location.path('/validation').replace();
						// event.preventDefault()可以阻止模板解析
					}
				}
	});
});
